package lec07.pm;

import java.util.Scanner;
import java.util.Stack;

public class BraceMatcherTestor {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for (int i = 0; i < n; i++) {
			Stack<Character> s = new Stack<>();
			String cur = cin.next();
			boolean ok = true;
			for (int j = 0; j < cur.length(); j++) {
				char c = cur.charAt(j);
				if (c == '(' || c == '[')
					s.push(c);
				else if (c == ')') {
					if (!s.isEmpty() && s.peek() == '(')
						s.pop();
					else {
						ok = false;
						break;
					}

				} else if (c == ']') {
					if (!s.isEmpty() && s.peek() == '[')
						s.pop();
					else {
						ok = false;
						break;
					}

				}
			}
			if (ok && s.isEmpty()) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}

		}

		cin.close();

	}
}
